package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.entity.Dish;
import com.itheima.service.IDishService;
import com.itheima.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private IDishService dishService;

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        return Result.success(dishService.getById(id));
    }

    @PostMapping
    public Result saveDish(@RequestBody Dish dish){
        return dishService.saveDish(dish);
    }

    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status,@RequestParam Long ids){
        return dishService.status(status,ids);
    }

    @DeleteMapping
    public Result deleteById(@RequestParam Long ids){
        return dishService.deleteById(ids);
    }

    @GetMapping("/list")
    public Result list(@RequestParam Long categoryId){
        return dishService.listDish(categoryId);
    }

    @GetMapping("/page")
    public Result getPage(@RequestParam(value = "page") int page, @RequestParam(value = "pageSize")  int pageSize, @RequestParam(value = "name" ,required = false) String name){
        IPage dishServicePage = dishService.getPage(page, pageSize ,name);
        if (page >dishServicePage.getPages()){
            dishServicePage = dishService.getPage((int) dishServicePage.getPages(), pageSize ,name);
        }
        return Result.success(dishServicePage);
    }
}
